/**********************************************************
 * ServerBackupAction.java.java
 * Created by Ankur
 * Last modified Date: Sep 18, 2008 10:49:50 AM by Ankur
 * Revision: <svn revision #>
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 **********************************************************/

package com.printeverywhere.action;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.Logger;

import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.ShellUtil;

/**
 * The Class ServerBackupAction.
 */
public class ServerBackupAction extends AdminSystemTools {

	/** Logger for this class. */
	private static final Logger logger = Logger.getLogger(ServerBackupAction.class);

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/** The backup command. */
	private String backupCommand;

	/** The compress command. */
	private String compressCommand;

	/** The backup path. */
	private String backupPath;

	/** The back up dir. */
	private String backUpDir;

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	public String execute() {
		return SUCCESS;
	}

	/**
	 * Back up.
	 *
	 * @return the string
	 */
	public String backUp(){
		if (logger.isDebugEnabled()) {
			logger.debug("backUp() - start"); 
		}
		String  script=null;
		try {
			script = (backupCommand + AdminConstants.BLANK + backupPath + AdminConstants.BLANK + backUpDir).intern();
			Process process= ShellUtil.runShellScript(script , null);
			if(process!=null){
				process.destroy();
			}
		} catch (IOException e) {
			logger.error("backUp()", e); 
			addActionError("Your Script: "+script);
			addActionError(e.getLocalizedMessage());
			if (logger.isDebugEnabled()) {
				logger.debug("backUp() - end"); 
			}
			return INPUT;
		} catch (InterruptedException e) {
			logger.error("backUp()", e); 
			addActionError("Could not save the file");
			addActionError(e.getLocalizedMessage());
			if (logger.isDebugEnabled()) {
				logger.debug("backUp() - end");
			}
			return INPUT;
		}
		File file=new File(backupPath);
		
		if(!file.exists()){
			addActionError("Could not save the file");
			return INPUT;
		}
		if (logger.isDebugEnabled()) {
			logger.debug("backUp() - end"); 
		}
		return SUCCESS;
	}

	/**
	 * Gets the backup command.
	 *
	 * @return the backup command
	 */
	public String getBackupCommand() {
		return backupCommand;
	}

	/**
	 * Sets the backup command.
	 *
	 * @param backupCommand the new backup command
	 */
	public void setBackupCommand(String backupCommand) {
		this.backupCommand = backupCommand;
	}

	/**
	 * Gets the compress command.
	 *
	 * @return the compress command
	 */
	public String getCompressCommand() {
		return compressCommand;
	}

	/**
	 * Sets the compress command.
	 *
	 * @param compressCommand the new compress command
	 */
	public void setCompressCommand(String compressCommand) {
		this.compressCommand = compressCommand;
	}

	/**
	 * Gets the backup path.
	 *
	 * @return the backup path
	 */
	public String getBackupPath() {
		return backupPath;
	}

	/**
	 * Sets the backup path.
	 *
	 * @param backupPath the new backup path
	 */
	public void setBackupPath(String backupPath) {
		this.backupPath = backupPath;
	}

	/**
	 * Gets the back up dir.
	 *
	 * @return the back up dir
	 */
	public String getBackUpDir() {
		return backUpDir;
	}

	/**
	 * Sets the back up dir.
	 *
	 * @param backUpDir the new back up dir
	 */
	public void setBackUpDir(String backUpDir) {
		this.backUpDir = backUpDir;
	}
}